import 'package:flutter/material.dart';
import 'package:geomancy/enums/index.dart';
import 'package:geomancy/models/festival_article_list_model.dart';
import 'package:geomancy/models/festival_list_model.dart';
import 'package:geomancy/pages/calendar/almanac/animal_luck.dart';
import 'package:geomancy/pages/calendar/almanac/good_day.dart';
import 'package:geomancy/pages/calendar/almanac/good_day_calendar.dart';
import 'package:geomancy/pages/calendar/almanac/good_day_result.dart';
import 'package:geomancy/pages/calendar/almanac/good_occasion.dart';
import 'package:geomancy/pages/calendar/almanac/nouns.dart';
import 'package:geomancy/pages/calendar/festival/article.dart';
import 'package:geomancy/pages/calendar/festival/desc.dart';
import 'package:geomancy/pages/first_install_app/index.dart';
import 'package:geomancy/pages/frame/index.dart';
import 'package:geomancy/pages/health/health_human_rhythm.dart';
import 'package:geomancy/pages/health/health_time_maintenance.dart';
import 'package:geomancy/pages/health/result.dart';
import 'package:geomancy/pages/login/code_input.dart';
import 'package:geomancy/pages/login/phone_code_login.dart';
import 'package:geomancy/pages/my/record_list.dart';
import 'package:geomancy/pages/my/system_message_detail.dart';
import 'package:geomancy/pages/my/system_message_list.dart';
import 'package:geomancy/pages/position_tips/index.dart';
import 'package:geomancy/pages/register/avatar.dart';
import 'package:geomancy/pages/register/sex.dart';
import 'package:geomancy/pages/register/birthday.dart';
import 'package:geomancy/pages/register/nickname.dart';
import 'package:geomancy/pages/health/index.dart';
import 'package:geomancy/pages/health/position_tips.dart';
import 'package:geomancy/pages/relations/result.dart';
import 'package:geomancy/pages/relations/result_detail.dart';
import 'package:geomancy/pages/setting/account_safe.dart';
import 'package:geomancy/pages/setting/edit_user_info.dart';
import 'package:geomancy/pages/setting/feedback.dart';
import 'package:geomancy/pages/setting/index.dart';
import 'package:geomancy/pages/splash/index.dart';
import 'package:geomancy/pages/start_compass/index.dart';
import 'package:geomancy/pages/study/result.dart';
import 'package:geomancy/pages/study/result_detail.dart';
import 'package:geomancy/pages/vip/index.dart';
import 'package:geomancy/pages/wealth/result.dart';
import 'package:geomancy/pages/wealth/result_detail.dart';
import 'package:geomancy/pages/webview_page/index.dart';
import 'package:go_router/go_router.dart';

class _MyRouterObserver extends RouteObserver<MaterialPageRoute<dynamic>> {
  // @override
  // void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
  //   route.navigator?.context.read<SystemProvider>().setCurrentRoute = route;
  // }
  // @override
  // void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
  //   route.navigator?.context.read<SystemProvider>().setCurrentRoute = previousRoute;
  // }
  //
  // @override
  // void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
  //   route.navigator?.context.read<SystemProvider>().setCurrentRoute = previousRoute;
  // }
  //
  // @override
  // void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
  //   newRoute?.navigator?.context.read<SystemProvider>().setCurrentRoute = newRoute;
  // }
}

class AppRouter {
  static enterFromBottom(BuildContext context, GoRouterState state, Widget page) {
    return CustomTransitionPage<void>(
      key: state.pageKey,
      child: page,
      transitionDuration: const Duration(milliseconds: 250),
      transitionsBuilder: (ct, animation, secondaryAnimation, Widget child) {
        return SlideTransition(
          position: Tween(
              begin: const Offset(0, 1),
              end: Offset.zero
          ).animate(animation),
          child: child,
        );
      },
    );
  }
  static enterFadeIn(BuildContext context, GoRouterState state, Widget page) {
    return CustomTransitionPage<void>(
      key: state.pageKey,
      child: page,
      transitionDuration: const Duration(milliseconds: 250),
      transitionsBuilder: (ct, animation, secondaryAnimation, Widget child) {
        return FadeTransition(
          opacity: animation,
          child: child,
        );
      },
    );
  }

  static final firstInstallRouters = GoRouter(
      observers: [
        _MyRouterObserver()
      ],
      // navigatorKey: navigatorKey,
      initialLocation: '/',
      routes: [
        GoRoute(
            path: '/',
            builder: (context, state) => const FirstInstallAppPage()
        ),
        GoRoute(
            path: '/web_view',
            builder: (context, state) {
              final m = state.extra as Map<String, dynamic>;
              String url = m['url'];
              return WebViewPage(url: url);
            }
        ),
      ]
  );

  static final routers = GoRouter(
    observers: [
      _MyRouterObserver()
    ],
    initialLocation: '/splash',
    routes: [
      GoRoute(
        path: '/splash',
        pageBuilder: (BuildContext context, GoRouterState state) {
          return enterFadeIn(context, state, const SplashPage());
        }
      ),
      GoRoute(
        path: '/',
        pageBuilder: (BuildContext context, GoRouterState state) {
          return enterFadeIn(context, state, const AppFramePage());
        },
      ),
      GoRoute(
        path: '/phone_code_login',
        pageBuilder: (BuildContext context, GoRouterState state) {
          return enterFromBottom(context, state, const PhoneCodeLoginPage());
        },
      ),
      GoRoute(
        path: '/code_input',
        builder: (context, state) => const CodeInputPage()
      ),
      GoRoute(
        path: '/register_nickname',
        builder: (context, state) => const RegisterNicknamePage()
      ),
      GoRoute(
        path: '/register_sex',
        builder: (context, state) => const RegisterSexPage()
      ),
      GoRoute(
        path: '/register_birthday',
        builder: (context, state) => const RegisterBirthdayPage()
      ),
      GoRoute(
        path: '/register_avatar',
        builder: (context, state) => const RegisterAvatarPage()
      ),
      GoRoute(
          path: '/position_tips',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            PositionTipsSource source = m['source'];
            String? birthday = m['birthday'];
            return PositionTipsPage(source: source, birthday: birthday);
          }
      ),
      GoRoute(
        path: '/start_compass',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          CompassSource source = m['source'];
          String? birthday = m['birthday'];
          String? sex = m['sex'];
          return StartCompassPage(source: source, birthday: birthday, sex: sex);
        }
      ),
      GoRoute(
        path: '/health_index',
        builder: (context, state) => const HealthIndexPage()
      ),
      GoRoute(
        path: '/health_position_tips',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          String birthday = m['birthday'];
          String sex = m['sex'];
          return HealthPositionTipsPage(birthday: birthday, sex: sex);
        }
      ),
      GoRoute(
        path: '/health_result',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          CompassSource source = m['source'];
          double direction = m['direction'];
          String birthday = m['birthday'];
          String sex = m['sex'];
          return HealthResultPage(source: source, direction: direction, birthday: birthday, sex: sex);
        }
      ),
      GoRoute(
        path: '/health_time_maintenance',
        builder: (context, state) => const HealthTimeMaintenance()
      ),
      GoRoute(
        path: '/health_human_rhythm',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          String birthday = m['birthday'];
          return HealthHumanRhythmPage(birthday: birthday);
        }
      ),
      GoRoute(
        path: '/wealth_result',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          CompassSource source = m['source'];
          double direction = m['direction'];
          return WealthResultPage(source: source, direction: direction);
        }
      ),
      GoRoute(
        path: '/wealth_result_detail',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          CompassSource source = m['source'];
          double direction = m['direction'];
          int index = m['index'];
          return WealthResultDetailPage(source: source, direction: direction, index: index);
        }
      ),
      GoRoute(
        path: '/study_result',
        builder: (context, state) {
          final m = state.extra as Map<String, dynamic>;
          CompassSource source = m['source'];
          double direction = m['direction'];
          String birthday = m['birthday'];
          return StudyResultPage(source: source, direction: direction, birthday: birthday);
        }
      ),
      GoRoute(
          path: '/study_result_detail',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            CompassSource source = m['source'];
            double direction = m['direction'];
            int index = m['index'];
            return StudyResultDetailPage(source: source, direction: direction, index: index);
          }
      ),
      GoRoute(
          path: '/relations_result',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            CompassSource source = m['source'];
            double direction = m['direction'];
            String birthday = m['birthday'];
            return RelationsResultPage(source: source, direction: direction, birthday: birthday);
          }
      ),
      GoRoute(
          path: '/relations_result_detail',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            CompassSource source = m['source'];
            double direction = m['direction'];
            int index = m['index'];
            return RelationsResultDetailPage(source: source, direction: direction, index: index);
          }
      ),
      GoRoute(
          path: '/vip',
          builder: (context, state) => const VipIndexPage()
      ),
      GoRoute(
          path: '/setting',
          builder: (context, state) => const SettingIndexPage()
      ),
      GoRoute(
          path: '/animal_luck',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            int animalIndex = m['animalIndex'];
            return AnimalLuckPage(animalIndex: animalIndex);
          }
      ),
      GoRoute(
          path: '/good_occasion',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            DateTime date = m['date'];
            return GoodOccasionPage(date: date);
          }
      ),
      GoRoute(
          path: '/good_day',
          builder: (context, state) => const GoodDayPage()
      ),
      GoRoute(
          path: '/good_day_result',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            String type = m['type'];
            String status = m['status'];
            return GoodDayResultPage(type: type, status: status);
          }
      ),
      GoRoute(
          path: '/good_day_calendar',
          builder: (context, state) => const GoodDayCalendarPage()
      ),
      GoRoute(
          path: '/nouns_list',
          builder: (context, state) => const NounsListPage()
      ),
      GoRoute(
          path: '/festival_desc',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            FestivalItemModel festival = m['festival'];
            return CalendarFestivalDescPage(festival: festival);
          }
      ),
      GoRoute(
          path: '/festival_article',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            Article article = m['article'];
            return CalendarFestivalArticlePage(article: article);
          }
      ),
      GoRoute(
          path: '/webview',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            String url = m['url'];
            return WebViewPage(url: url);
          }
      ),
      GoRoute(
          path: '/setting_edit_user_info',
          builder: (context, state) => const SettingEditUserInfoPage()
      ),
      GoRoute(
          path: '/setting_account_safe',
          builder: (context, state) => const SettingAccountSafePage()
      ),
      GoRoute(
          path: '/setting_feedback',
          builder: (context, state) => const SettingFeedbackPage()
      ),
      GoRoute(
          path: '/record_list',
          builder: (context, state) => const RecordListPage()
      ),
      GoRoute(
          path: '/system_message_list',
          builder: (context, state) => const SystemMessageListPage()
      ),
      GoRoute(
          path: '/system_message_detail',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            int id = m['id'];
            return SystemMessageDetailPage(id: id);
          }
      ),
      GoRoute(
          path: '/web_view',
          builder: (context, state) {
            final m = state.extra as Map<String, dynamic>;
            String url = m['url'];
            return WebViewPage(url: url);
          }
      ),
    ]
  );
}